package com.hanlin.backend.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanlin.backend.common.dto.EnumDto;
import com.hanlin.backend.common.entity.HlEnum;
import com.hanlin.backend.common.mapper.HlEnumMapper;
import com.hanlin.backend.common.service.HlEnumService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 枚举表 服务实现类
 * </p>
 *
 * @author hl.yuan
 * @since 2021-01-06
 */
@Service
public class HlEnumServiceImpl extends ServiceImpl<HlEnumMapper, HlEnum> implements HlEnumService {

    @Override
    public List<HlEnum> getEnumByCatalogAndType(String catalog, String type) {
        QueryWrapper<HlEnum> wrapper = new QueryWrapper<>();
        wrapper.eq("catalog", catalog);
        wrapper.eq("type", type);
        wrapper.orderByAsc("sort");
        List<HlEnum> enums = getBaseMapper().selectList(wrapper);
        return enums;
    }

    @Override
    public IPage<HlEnum> getTableList(EnumDto enumDto) {
        IPage iPage = enumDto.buildPage();
        IPage<HlEnum> list = getBaseMapper().getTableList(iPage, enumDto);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEnum(HlEnum enumDto) {
        saveOrUpdate(enumDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEnum(int[] id) {
        // 当ID集合数据大于0，才进行删除
        if (id.length >= 0) {
            UpdateWrapper<HlEnum> wrapper = new UpdateWrapper();
            wrapper.in("id", id);
            getBaseMapper().delete(wrapper);
        }
    }
}
